package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.port.fee.entity.FeeRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 费用记录Mapper接口
 */
@Mapper
public interface FeeRecordMapper extends BaseMapper<FeeRecord> {

    /**
     * 按费用类型统计收入
     */
    @Select("SELECT t.fee_name AS feeName, SUM(r.amount) AS totalAmount " +
            "FROM fee_record r " +
            "JOIN fee_type t ON r.fee_type_id = t.fee_type_id " +
            "WHERE r.pay_status = 1 " +
            "GROUP BY r.fee_type_id, t.fee_name")
    List<Map<String, Object>> statisticsByFeeType();

    /**
     * 按客户统计费用
     */
    @Select("SELECT c.customer_name AS customerName, SUM(r.amount) AS totalAmount " +
            "FROM fee_record r " +
            "JOIN customer c ON r.customer_id = c.customer_id " +
            "WHERE r.pay_status = 1 " +
            "GROUP BY r.customer_id, c.customer_name")
    List<Map<String, Object>> statisticsByCustomer();

    /**
     * 获取货物相关的费用总额
     */
    @Select("SELECT IFNULL(SUM(amount), 0) FROM fee_record WHERE cargo_id = #{cargoId}")
    BigDecimal getTotalFeeByCargoId(@Param("cargoId") Long cargoId);
    
    /**
     * 获取已支付的总收入
     */
    @Select("SELECT IFNULL(SUM(amount), 0) FROM fee_record WHERE pay_status = 1")
    BigDecimal getTotalPaidAmount();
}